package com.pomr.service.bind.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.pomr.dao.base.BaseDao;
import com.pomr.model.equipmentstorage.DeviceMatch;
import com.pomr.model.equipmentstorage.EquipmentStorageDetail;
import com.pomr.model.simstorage.SimStorageDetail;
import com.pomr.model.sys.User;
import com.pomr.service.bind.BindService;
@Service
public class BindServiceImpl implements BindService {
	@Autowired
	@Qualifier("baseDaoImpl")
	private BaseDao baseDao;

	@Override
	@Transactional(rollbackFor=Exception.class)
	public boolean bind(DeviceMatch device, User user) throws Exception {
		if(device.getEquipmentdetail().getId()==null || device.getSimdetail().getId()==null){
			throw new Exception("SIM卡或设备信息缺失");
		}
		device.setBindTime(new Date());
		device.setUser(user);
		if(baseDao.add(device)){
			//绑定成功 更改SIM卡以及设备状态
			String hql = "update EquipmentStorageDetail set equipmentStatus.id=3 where id="+device.getEquipmentdetail().getId()+" and equipmentStatus.id!=2 and equipmentStatus.id!=3";
			if(!baseDao.editHql(hql)){
				throw new Exception("绑定失败,更新设备状态失败");
			}
			hql = "update SimStorageDetail set equipmentStatus.id=3 where id="+device.getSimdetail().getId()+" and equipmentStatus.id!=2 and equipmentStatus.id!=3";
			if(!baseDao.editHql(hql)){
				throw new Exception("绑定失败,更新SIM卡状态失败");
			}
		}else{
			throw new Exception("绑定失败");
		}
		return true;
	}

	@Override
	@Transactional(rollbackFor=Exception.class)
	public boolean noBind(String id, String type) throws Exception {
		String hql = null;
		if(type.equals("equip")){//通过设备解绑
			hql = "from DeviceMatch d left join fetch d.equipmentdetail left join fetch d.simdetail "
					+ "where d.equipmentdetail.id="+id+" and d.noBindTime=null and d.equipmentdetail.equipmentStatus.id!=2 "
					+ "and d.equipmentdetail.equipmentCon.id!=3";//未领取 未被清库 未解绑
		}
		if(type.equals("sim")){//通过sim卡解绑
			hql = "from DeviceMatch d left join fetch d.equipmentdetail left join fetch d.simdetail "
					+ "where d.simdetail.id="+id+" and d.noBindTime=null and d.simdetail.equipmentStatus.id!=2 "
					+ "and d.simdetail.equipmentCon.id!=3";//未领取 未被清库 未解绑
		}
		List<DeviceMatch> devices = baseDao.findByHql(hql);
		if(devices!=null && devices.size()>0){
			DeviceMatch device = devices.get(0);
			//修改设备以及sim卡状态
			hql = "update SimStorageDetail set equipmentStatus.id = 1 where id = "+device.getSimdetail().getId();
			if(!baseDao.editHql(hql)){
				throw new Exception("更新SIM卡状态失败");
			}
			hql = "update EquipmentStorageDetail set equipmentStatus.id = 1 where id = "+device.getEquipmentdetail().getId();
			if(!baseDao.editHql(hql)){
				throw new Exception("更新设备状态失败");
			}
			//更新绑定记录
			device.setNoBindTime(new Date());
			if(!baseDao.editEntity(device)){
				throw new Exception("更新解绑时间失败");
			}
		}
		return true;
	}

	@Override
	public SimStorageDetail searchBindSim(String eid) throws Exception {
		String hql = "from SimStorageDetail where id =( select d.simdetail.id from DeviceMatch d where d.equipmentdetail.id="+eid+" and d.noBindTime=null)";
		List<SimStorageDetail> details = baseDao.findByHql(hql);
		if(details!=null && details.size()>0){
			return details.get(0);
		}
		return null;
	}

	@Override
	public List<SimStorageDetail> searchSimLd(String networkid, String manufacturerid, User user) throws Exception {
		String hql = "from SimStorageDetail where simStorage.networkOperator.id="+networkid
				+" and simStorage.manufacturer.id="+manufacturerid
				+" and simStorage.agency.id="+user.getAgency().getId()
				+ " and (equipmentCon.id!=3 and equipmentCon.id!=2)"//SIM卡不能是清库或者损坏的
				+" and (equipmentStatus.id!=3 and equipmentStatus.id!=2)";//SIM卡不能是已经绑定或者领用的
		
		return baseDao.findByHql(hql);
	}

}
